<!-- 扫码记录 -->
<template>
	<view class="">
		<u-sticky offset-top="0">
			<common-search
				class="search"
				v-model="queryParam.billNo" 
				:screenShow="screenShow" 
				:isScreen="false"
				@screenclick="screenclick"
				@searchMsg="searchMsg"
				placeholder="请输入单据编号"
			></common-search>
		</u-sticky>
		<view class="page" :style="!isData?'display:flex;width: 100vw;height:100vh;align-items:center;justify-content: center;':''">
			<template v-if="isData">
				<view class="list" v-for="(item,index) in dataList" :key="index">
					<view class="">
						<view style="display: flex;align-items: center;">
							<view class=""><u-icon :name="item.isClick?'minus-circle':'plus-circle'" @click="handleCircle(item, index)"></u-icon></view>
							<view class="">
								<view class="option">
									<view class="lf">单据编号</view>
									<view class="rl">{{ item.billNo }}</view>
								</view>
								<view class="option">
									<view class="lf">单据分类</view>
									<view class="rl">{{ item.billType_dictText }}</view>
								</view>
								<view class="option">
									<view class="lf">申请人/报销人</view>
									<view class="rl">{{ item.reimburser_dictText }}</view>
								</view>
								<view class="option">
									<view class="lf">本次付款/支付金额(元)</view>
									<view class="rl" >{{ item.paymentAmount }}</view>
								</view>
								<view class="option">
									<view class="lf">实际支付总金额(元)</view>
									<view class="rl" >{{ item.actualPaymentAmount }}</view>
								</view>
								<view class="option">
									<view class="lf">剩余未支付金额(元)</view>
									<view class="rl" >{{ item.remainingPaymentAmount }}</view>
								</view>
								<view class="option">
									<view class="lf">交单状态</view>
									<view class="rl" >{{ item.recordStatus_dictText }}</view>
								</view>
								<view class="option">
									<view class="lf">交单时间</view>
									<view class="rl" >{{ item.againstDocumentsTime }}</view>
								</view>
								
							</view>
						</view>
						<view style="margin-left: 10px;margin-top: 10px; border-top: 1px solid #ccc; padding: 10rpx 20rpx; 0 20rpx" v-show="item.isClick">
							<view style="color: #303133;">支付进度一览</view>
							<template v-if="item.child && item.child.length">
								<view class="list" v-for="(value,ind) in item.child" :key="ind">
									<view class="">
										<view class="option">
												<view class="lf">单据编号</view>
												<view class="rl" >{{ value.billNo }}</view>
											</view>
											<view class="option">
												<view class="lf">本次付款金额(元)</view>
												<view class="rl" >{{ value.paymentAmount }}</view>
											</view>
											<view class="option">
												<view class="lf">实际支付金额(元)</view>
												<view class="rl" >{{ value.actualPaymentAmount }}</view>
											</view>
											<view class="option">
												<view class="lf">付款进度</view>
												<view class="rl" >{{ value.billSchedule_dictText }}</view>
											</view>
											<view class="option">
												<view class="lf">支付方式</view>
												<view class="rl" >{{ value.billPayType_dictText }}</view>
											</view>
											<view class="option">
												<view class="lf">银行电子回单</view>
												<view class="rl" v-if="!value.attachment" style="font-size: 12px;font-style: italic;">无文件</view>
												<view class="rl" v-else-if="value.attachment=='ERROR'" style="font-size: 12px;font-style: italic;">电子回单已过期，请联系出纳</view>
												<view class="rl"  v-else style="color: #3C9CFF;font-size: 12px;font-style: italic;"  @click="download(value.attachment)">
													下载
												</view>
											</view>
										</view>
									</view>
							</template>
								<view v-else>
									<u-empty
										mode="data"
										icon="/static/empty/data.png"
										text="暂无支付进度，请耐心等待财务为您处理~"
									>
									</u-empty>
								</view>
						</view>
					</view>
				</view>
				<!-- 加载更多显示 -->
				<view class=""><u-loadmore :status="statusLoadmore" /></view>
			</template>
			<view v-else>
				<u-empty
					mode="data"
					icon="/static/empty/data.png"
				>
				</u-empty>
			</view>
		</view>
		
		<u-toast ref="uToast"></u-toast>
	</view>
</template>
<script>
	import { erpCompanyFilesList } from '@/api/index.js';
	import CommonSearch from '../../components/CommonSearch.vue'
	import http from '@/utils/request/index.js'
	import { imageUrl } from '@/utils/common.js'
	export default {
		components: {
			CommonSearch
		},
		data() {
			return {
				page: {
					pageNo: 1,
					pageSize: 10,
				},
				dataList: [] ,// 列表
				isData: true, // 判断是否有数据  true 代表有 false 代表无
				statusLoadmore: 'loadmore', // 通过status设置组件的状态，加载前值为loadmore，加载中为loading，没有数据为nomore
				screenShow: false, // 筛选弹窗
				// 传的参数
				queryParam: {
					billNo: ''
				}
			}
		},
		onLoad(options) {
			this.geterpOmcTeamManagerList() // 获取列表
		},
		methods: {
			// 获取列表
			geterpOmcTeamManagerList () {
				this.statusLoadmore = 'loading' // 加载中
				var params = {
					column: 'createTime',
					order: 'desc',
					pageNo: this.page.pageNo,
					pageSize: this.page.pageSize,
					recordStatus: 'success,fail'
				}
				if (this.queryParam.billNo) {
					params.billNo = '*' + this.queryParam.billNo + '*'
				}
				http.get('/api/erp/finance/record/erpFinancePaymentAgainstDocumentsRecord/list', params).then(res => {
					if (res.success) {
						 // 判断首次加载是否有数据
						if (this.page.pageNo == 1){
							if (res.result.records&&res.result.records.length) {
								this.isData = true
							} else {
								this.isData = false
							}
						}
						if (res.result.records&&res.result.records.length){
							if (res.result.records.length >= 10 ) {
								this.statusLoadmore = 'loadmore'
							} else {
								this.statusLoadmore = 'nomore'
							}
						}else {
							this.statusLoadmore = 'nomore'
						}
						if (this.page.pageNo == 1){
							this.dataList = res.result.records
						} else {
							this.dataList = this.dataList.concat(res.result.records)
						}
					}
				})
			},
			// 点击展开和收缩
			handleCircle (item, index) {
				this.dataList.map((value, ind) => {
					if (ind != index) {
						this.$set(this.dataList[ind], 'isClick', false)
						// this.dataList[ind].isClick = false
					}
				})
				if (item.isClick) {
					// this.dataList[index].isClick = false
					this.$set(this.dataList[index], 'isClick', false)
				} else {
					// this.dataList[index].isClick = true
					this.$set(this.dataList[index], 'isClick', true)
				}
			},
			// 点击编号
			onContarctName (item) {
				uni.redirectTo({
					url:'/pages/reportFilesList/ErpFinanceCollectMoneyApplicationBillForm?id=' + item.id
				})
			},
			
			// 点击筛选
			screenclick () {
				this.screenShow = !this.screenShow
			},
			// 输入内容
			searchMsg () {
				this.page.pageNo = 1 // 页数归1
				this.dataList = [] // 清空列表
				this.geterpOmcTeamManagerList() // 获取列表
			},
			
			download(url) {
			  var that = this
				var urlText = imageUrl(url)
			  // #ifndef APP-PLUS
			  that.handleDownLoad(urlText)
			  // #endif
			  // #ifdef APP-PLUS
			  that.downLoadApp(urlText)
			  // #endif
			},
			
			// uniapp 原生api -- uni.downloadFile下载
			handleDownLoad(url) {
			  uni.downloadFile({
			    url: url,
			    success: (result) => {
			      if (result.statusCode === 200) {
			        //文件保存到本地
			        uni.saveFile({
			          tempFilePath: result.tempFilePath, //临时路径
			          success: function (ress) {
			            uni.showToast({
			              icon: 'none',
			              mask: true,
			              title: '文件已保存：' + ress.savedFilePath, //保存路径
			              duration: 3000,
			            })
			          },
			        })
			        // 文件保存到本地
			        uni.openDocument({
			          filePath: result.tempFilePath,
			          showMenu: true,
			          success: function (res) {
			            console.log('打开文档成功')
			          },
			        })
			      }
			    },
			    fail: (err) => {
			      uni.showToast({
			        icon: 'none',
			        mask: true,
			        title: '失败请重新下载',
			      })
			    },
			  })
			},
			// H5+API定义的应用沙盒里的plus.downloader下载
			downLoadApp(url) {
			  var name = url.split('static/').pop()
			  let dtask = plus.downloader.createDownload(
			    url,
			    {
			      //1.本地路径开头使用file://;
			      //2.Android平台本地绝对路径为"storage/emulated/0",就是用户文件管理器能看到的了;
			      //3.创建"xxx"作为文件夹名称，后缀是用于文件命名和格式修改，大家可以使用变量。
			      filename: '_downloads/' + name, //利用保存路径，实现下载文件的重命名
			    },
			    function (d, status) {
			      //d为下载的文件对象;status下载状态
			      if (status == 200) {
			        //下载成功
			        //d.filename是文件在保存在本地的相对路径，使用下面的API可转为平台绝对路径
			        // let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
			        // plus.runtime.openFile(d.filename); //选择软件打开文件
			        uni.showToast({
			          icon: 'none',
			          mask: true,
			          title: '文件已保存：' + d.filename, //保存路径
			          duration: 2000,
			        })
			        this.timer = setTimeout(() => {
			          clearTimeout(this.timer)
			          this.timer = null
			          // 文件保存到本地
			          uni.openDocument({
			            filePath: d.filename,
			            showMenu: true,
			            success: function (res) {
			              console.log('打开文档成功')
			            },
			          })
			        }, 2000)
			      } else {
			        //下载失败
			        uni.showToast({
			          icon: 'none',
			          title: '下载失败',
			          mask: true,
			          duration: 2000,
			        })
			        plus.downloader.clear() //清除下载任务
			      }
			    },
			  )
			  dtask.start() //启用
			},
		},
		onReachBottom() {
			if (this.statusLoadmore == 'nomore') return false
			this.page.pageNo = ++ this.page.pageNo;
			this.geterpOmcTeamManagerList()
		}
		
	}
</script>
<style lang="scss" scoped>
.page {
	padding: 0 20rpx 20rpx 20rpx;
	.list{
		border-radius: 10rpx;
		box-shadow: 0px 4px 20px 0px rgba(0,0,0,0.1);
		padding: 12rpx 20rpx 20rpx 20rpx;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 20rpx;
	}
}

// 按钮
.btn{
	position: fixed;
	bottom: 20rpx;
	width: 95%;
}
</style>

